package com.zackeus.common.utils;

import java.util.Collection;
import java.util.Map;

/**
 * @name: ObjectUtils
 * @author: zz
 * @date: 2019/8/12
 */
public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {

    private ObjectUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * @description 判断对象是否null
     * @param
     * @author zz
     * @time 2019/8/12 9:40
     * @return
     */
    public static boolean isNull(Object obj) {
        return obj == null;
    }

    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }

    /**
     * @description 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
     * @param
     * @author zz
     * @time 2019/8/9 16:26
     * @return
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null)
            return Boolean.TRUE;

        if (obj instanceof CharSequence)
            return ((CharSequence) obj).length() == 0;

        if (obj instanceof Collection)
            return ((Collection) obj).isEmpty();

        if (obj instanceof Map)
            return ((Map) obj).isEmpty();

        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return Boolean.TRUE;
            }
            boolean empty = Boolean.TRUE;
            for (Object o : object) {
                if (!isEmpty(o)) {
                    empty = Boolean.FALSE;
                    break;
                }
            }
            return empty;
        }
        return Boolean.FALSE;
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

}
